package com.ruoyi.client.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.client.constants.LogConstants;
import com.ruoyi.client.domain.entity.*;
import com.ruoyi.client.domain.vo.OperVo;
import com.ruoyi.client.mapper.BlogOperMapper;
import com.ruoyi.client.service.IBlogArticleService;
import com.ruoyi.client.service.IBlogOperService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.client.service.IBlogUserService;
import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.json.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 活动记录表 服务实现类
 * </p>
 *
 * @author fz
 * @since 2025-03-13
 */
@Service
public class BlogOperServiceImpl extends ServiceImpl<BlogOperMapper, BlogOper> implements IBlogOperService {
    @Autowired
    IBlogUserService userService;
    @Autowired
    IBlogArticleService articleService;
    @Async
    @EventListener
    public void addOper(BlogOper oper){
        handleBusiness(oper);

        save(oper);
    }

    private void handleBusiness(BlogOper oper) {
        String targetClass = oper.getTargetClass();
        switch (oper.getBusinessNum()){
            // 发布
            case 0:{
                BlogArticle article = JsonUtils.parseObject(oper.getTargetJson(), BlogArticle.class);
                oper.setIcon("FileOutlined");
                Integer status = article.getStatus();
                oper.setTargetId(article.getId());
                oper.setStatus(status);
                if (status == 1) {
                    oper.setDescription(String.format("您保存了一篇草稿《%s》",article.getTitle()));
                } else if (status == 2) {
                    oper.setDescription(String.format("您发布了一篇文章《%s》，待审核中",article.getTitle()));
                } else if (status == 3) {
                    oper.setDescription(String.format("您发布的一篇文章《%s》，审核未通过",article.getTitle()));
                } else {
                    oper.setDescription(String.format("您发布了一篇文章《%s》，审核通过，已发布",article.getTitle()));
                }
                break;
            }
            // 关注
            case 1:{
                oper.setIcon(LogConstants.ICON_PUBLISH);
                BlogAttention o = JsonUtils.parseObject(oper.getTargetJson(), BlogAttention.class);
                Long targetUserId = o.getTargetUserId();
                BlogUser targetUser = userService.getById(targetUserId);
                oper.setTargetId(targetUserId);
                oper.setDescription(String.format("您关注了作者——%s",targetUser.getNickname()));
                break;
            }
            // 点赞
            case 2:{
                BlogLike like = JsonUtils.parseObject(oper.getTargetJson(),BlogLike.class);
                BlogArticle article = articleService.getById(like.getTargetId());
                oper.setIcon(LogConstants.ICON_LIKE);
                oper.setUrl(like.getUrl());
                oper.setTargetId(like.getTargetId());
                if (like.getIsLike()) {
                    oper.setDescription(String.format("您对《%s》进行了点赞",article.getTitle()));
                }
                else {
                    oper.setDescription(String.format("您对《%s》取消了点赞",article.getTitle()));
                }
                break;
            }
            // 收藏
            case 3:{
                BlogCollect collect = JsonUtils.parseObject(oper.getTargetJson(), BlogCollect.class);
                BlogArticle article = articleService.getById(collect.getTargetId());
                oper.setIcon(LogConstants.ICON_COLLECTION);
                oper.setUrl(collect.getUrl());
                oper.setTargetId(collect.getTargetId());
                if (collect.getIsCollect()){
                    oper.setDescription(String.format("您收藏了文章——《%s》",article.getTitle()));
                }
                else {
                    oper.setDescription(String.format("您取消收藏文章——《%s》",article.getTitle()));
                }
                break;
            }
            // 修改
            case 4:{
                oper.setIcon(LogConstants.ICON_MODIFY);
                BlogUser user = JsonUtils.parseObject(oper.getTargetJson(), BlogUser.class);
                oper.setTargetId(user.getId());
                oper.setDescription(String.format("您修改了资料"));
                break;
            }
            // 评论
            case 5:{
                oper.setIcon(LogConstants.ICON_COMMENT);
                BlogComment comment = JsonUtils.parseObject(oper.getTargetJson(), BlogComment.class);
                oper.setTargetId(comment.getTargetId());
                oper.setUrl(comment.getUrl());
                BlogArticle article = articleService.getById(comment.getTargetId());
                oper.setDescription(String.format("您在《%s》，新增了一条评论",article.getTitle()));
                break;
            }
        }
    }

    @Override
    public List<OperVo> getLatelyByUserId(Long userId) {
        List<BlogOper> list = list(Wrappers.<BlogOper>lambdaQuery()
            .eq(BlogOper::getUserId, userId)
            .orderBy(true,false,BlogOper::getCreateTime)
        );
        ArrayList<OperVo> arrayList = new ArrayList<>();
        list.forEach(v -> {
            OperVo operVo = new OperVo();
            MapstructUtils.convert(v,operVo);
            arrayList.add(operVo);
        });
        return arrayList;
    }
}
